<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>全排列</title>
</head>

<body>
  <script>

    var permute = function (nums) {
      let result = []
      let used = {}
      function dfs(arr) {
        if (arr.length === nums.length) {
          result.push(arr.slice())
        }
        for (let i = 0; i < nums.length; i++) {
          if (used[i]) {
            continue
          }
          arr.push(nums[i])
          used[i] = true
          dfs(arr)
          // 撤销上一次操作
          arr.pop()
          used[i] = false
        }
      }
      dfs([])
      return result
    };
    console.log(permute([1, 2, 3]));

  </script>
</body>

</html>